package cn.bgs.service.impl;

import cn.bgs.Exception.PhoenixException;
import cn.bgs.dao.ShoppingCartMapper;
import cn.bgs.pojo.ShoppingCart;
import cn.bgs.pojo.dto.ShoppingCartDTO;
import cn.bgs.service.ShoppingCartService;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    Logger log = LogManager.getLogger();

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public int deleteByPrimaryKey(Integer pkId) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int insert(ShoppingCart record) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int insertSelective(ShoppingCart record) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public ShoppingCart selectByPrimaryKey(Integer pkId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(ShoppingCart record) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int updateByPrimaryKey(ShoppingCart record) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void insertShoppingCart(ShoppingCartDTO record) throws PhoenixException {
        log.info("insertShoppingCart.ShoppingCartDTO::: " + record);
        // 1. 根据入参查询购物车里是否有数据
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setStudentBasicInfoStudentId(record.getStudentBasicInfoStudentId());
        shoppingCart.setGoodsId(record.getGoodsId());
        shoppingCart.setDeleteFlag("0");
        log.info("shoppingCartMapper.listByAll::: " + shoppingCart);
        //查询购物车方法
        List<ShoppingCart> listShoppingCart = shoppingCartMapper.listByAll(shoppingCart);
        log.info("shoppingCartMapper.listByAll-resp::: " + listShoppingCart);

        // ShoppingCart对象赋值
        //后面的对象赋值给前面的对象
        BeanUtils.copyProperties(record, shoppingCart);
        shoppingCart.setDeleteFlag("0");
        //获取当前时间为创建时间和修改时间
        Calendar now = Calendar.getInstance();
        shoppingCart.setCreateTime(now.getTime());
        shoppingCart.setUpdateTime(now.getTime());

        if (listShoppingCart.size() > 0) {
            // 2. 如果有数据， update 商品数量
            int count = listShoppingCart.get(0).getCount();
            shoppingCart.setCount(count + 1);
            shoppingCart.setPkId(listShoppingCart.get(0).getPkId());

            log.info("shoppingCartMapper.updateByPrimaryKeySelective Exception::: " + shoppingCart);
            try {
                shoppingCartMapper.updateByPrimaryKeySelective(shoppingCart);
            } catch (Exception e) {
                log.error("shoppingCartMapper.updateByPrimaryKeySelective Exception::: " + shoppingCart);
                throw new PhoenixException("9900", e.getMessage());
            }
        } else {
            // 3. 如果没有数据，insert 添加一条信息到购物车
            shoppingCart.setCount(1);
            log.info("shoppingCartMapper.insertSelective::: " + shoppingCart);
            try {
                shoppingCartMapper.insertSelective(shoppingCart);
            } catch (Exception e) {
                log.error("shoppingCartMapper.insertSelective::: " + shoppingCart);
                throw new PhoenixException("9900", e.getMessage());
            }
        }
    }
}
